﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.IM.DataAccess;
using WP.IM.DataEntity;
namespace WP.IM.BusinessRule
{
    public class ImInvMasterBRL : RuleBase
    {
        public ImInvMasterBRL()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region Insert rmCategory Valid
        public bool InsertValid(DBAccess dBAccess, ImInvMasterDEL imInvMasterDEL)
        {
            bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + ImInvMasterDEL.TABLENAME_IminvMaster + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, imInvMasterDEL.Tables[ImInvMasterDEL.TABLENAME_IminvMaster].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.AddNew))
                {
                    bReturn = false;
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                    }
                    break;
                }
            }
            if (!bReturn) return false; 

            string strSQL = "select * from iminvmaster where itemno='" + imInvMasterDEL.Tables[0].Rows[0]["ItemNo"] + "' and supname='" + imInvMasterDEL.Tables[0].Rows[0]["SupName"] + "'";
            DataRow drCheck = dBAccess.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                if (SysVar.ErrorMessage.Count == 0)
                {
                    SysVar.ErrorMessage.Add( "物料 已经存在.");
                }
                return false ;
            }
            else
            {
                return true ;
            }
        }
        #endregion

        #region Update rmCategory Valid
        public bool UpdateValid(DBAccess dBAccess, ImInvMasterDEL imInvMasterDEL)
        {
            bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

            if (IsExistedAndNotChanged(dBAccess, imInvMasterDEL.Tables[ImInvMasterDEL.TABLENAME_IminvMaster]))
            {
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + ImInvMasterDEL.TABLENAME_IminvMaster  + "'").DefaultView;
                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, imInvMasterDEL.Tables[ImInvMasterDEL.TABLENAME_IminvMaster].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }
            }
            else
            {
                bReturn = false;
            }
            return bReturn;
        }
        #endregion

        #region Delete MCity Valid
        public bool DeleteValid(DBAccess dBAccess, string strRecId, byte[] btOldRecStamp)
        {
            SysVar.ErrorMessage.Clear();
            string strErrMsg = "", strTmpSQL="";
            string strItemNo = DataFunctions.GetFieldValue(dBAccess,"iminvmaster", "ItemNo", "RecID='" + strRecId + "'");
            string strSQL = "";
            strTmpSQL = "select top 1 RecID from {0} where itemno='" + strItemNo + "'";
            strSQL = string.Format(strTmpSQL, "rmCategorySetBOMDetail");
            DataTable dtCheck = dBAccess.GetDataTable(strSQL);
            if (dtCheck.Rows.Count > 0)
            {
                strErrMsg = "已被使用，不能删除！";
            }
            else if (strErrMsg == "")
            {               
                strSQL = string.Format(strTmpSQL, "rmCategorySetBOMLabourCost");
                dtCheck = dBAccess.GetDataTable(strSQL);
                if (dtCheck.Rows.Count > 0)
                {
                    strErrMsg = "已被使用，不能删除！";
                }
            }
            if (strErrMsg == "")
            {
                strSQL = string.Format(strTmpSQL, "rmCategorySetBOMDetail");
                dtCheck = dBAccess.GetDataTable(strSQL);
                if (dtCheck.Rows.Count > 0)
                {
                    strErrMsg = "已被使用，不能删除！";
                }
            }
            if (strErrMsg == "")
            {
                strSQL = string.Format(strTmpSQL, "rmProductBOMDetail");
                dtCheck = dBAccess.GetDataTable(strSQL);
                if (dtCheck.Rows.Count > 0)
                {
                    strErrMsg = "已被使用，不能删除！";
                }
            }
            if (strErrMsg == "")
            {
                strSQL = string.Format(strTmpSQL, "rmProductBOMDetailRev");
                dtCheck = dBAccess.GetDataTable(strSQL);
                if (dtCheck.Rows.Count > 0)
                {
                    strErrMsg = "已被使用，不能删除！";
                }
            }
            if (strErrMsg == "")
            {
                strSQL = string.Format(strTmpSQL, "rmProductBOMLabourCost");
                dtCheck = dBAccess.GetDataTable(strSQL);
                if (dtCheck.Rows.Count > 0)
                {
                    strErrMsg = "已被使用，不能删除！";
                }
            }
            if (strErrMsg == "")
            {
                strSQL = string.Format(strTmpSQL, "rmProductBOMLabourCostRev");
                dtCheck = dBAccess.GetDataTable(strSQL);
                if (dtCheck.Rows.Count > 0)
                {
                    strErrMsg = "已被使用，不能删除！";
                }
            }

            if (strErrMsg != "")
            {
                SysVar.ErrorMessage.Add(strErrMsg);
                return false ;
            }
            if (!base.IsExistedAndNotChanged(dBAccess, "iminvMaster", strRecId, btOldRecStamp))
                return false;
            if (MsgBoxEx.Show(Msg.Select("您确定要删除此记录?", DataFunctions.GetMsg("SureToDeleteThisRecord")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region Field Check
        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);

            switch (strFieldName.ToUpper().Trim())
            {
                #region ItemNo
                case "ITEMNO":
                    if (formStatus == FormStatus.AddNew)
                        return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, ImInvMasterDEL.TABLENAME_IminvMaster, strFieldName, ref  strErrPrompt);
                    else
                    {
                        string strSQL="select itemno from iminvmaster where itemno='"+ strFieldValue +"' and recid!='"+ dr["RecID"] +"'";
                        DataRow drRow = dbcon.GetDataFirstRow(strSQL);
                        if (drRow != null)
                        {
                            strErrPrompt = "物料编码已存在。";
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                        
                    }
                #endregion
                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, ImInvMasterDEL.TABLENAME_IminvMaster , strFieldName, ref strErrPrompt);
            }
        }
        #endregion
    }
}
